package com.gdpu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdpu.api.CommonPage;
import com.gdpu.bo.UmsMemberDetail;
import com.gdpu.common.ApiException;
import com.gdpu.config.RedisConfig;
import com.gdpu.dao.PortalOmsCartItemDao;
import com.gdpu.dto.OmsCartItemDto;
import com.gdpu.mapper.OmsCartItemMapper;
import com.gdpu.mapper.PmsGoodsMapper;
import com.gdpu.model.*;
import com.gdpu.service.OmsCartItemSerivece;
import com.gdpu.service.UmsMemberService;
import com.gdpu.vo.OmsCartGoodInfoVo;
import com.gdpu.vo.OmsCartItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/5/29
 */
@Service
public class OmsCartItemSeriveceImpl  implements OmsCartItemSerivece {

    @Autowired
    private OmsCartItemMapper omsCartItemMapper;
    @Autowired
    private PmsGoodsMapper pmsGoodsMapper;
    @Autowired
    private PortalOmsCartItemDao PortalOmsCartItemDao;
    @Autowired
    private  PortalOmsCartItemDao portalOmsCartItemDao;
    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    OmsCartItemSeriveceImpl cpService;



    @Override
    public  CommonPage<OmsCartGoodInfoVo> list( Integer pageNum, Integer pageSize) {

        Integer offset = pageSize * (pageNum-1);
        List<OmsCartItemVo> omsCartItems = PortalOmsCartItemDao.list(umsMemberService.getCurrentMember().getId(),offset,pageSize);
        Long totalRecode = PortalOmsCartItemDao.queryRecordTotal();
        CommonPage commonPage = CommonPage.restPage(omsCartItems,pageNum,pageSize,totalRecode);
        return commonPage;
    }

    @Override
    public int add(Integer goodId) {
        int count = 0;

        OmsCartItem omsCartItem = new OmsCartItem();
        omsCartItem.setId(0);
        omsCartItem.setGoodId(goodId);
        omsCartItem.setQuantity(1);
        omsCartItem.setIsDeleted(0);
        omsCartItem.setIsCheck(0);
        omsCartItem.setUserId(umsMemberService.getCurrentMember().getId());

        PmsGoodsExample pmsGoodsExample = new PmsGoodsExample();
        pmsGoodsExample.createCriteria()
                .andIsDeletedEqualTo(0).andIdEqualTo(omsCartItem.getGoodId());
        List<PmsGoods> pmsGoodsList = pmsGoodsMapper.selectByExample(pmsGoodsExample);
        if (pmsGoodsList.isEmpty()){
            throw new ApiException("商品不存在");
        }else{
            PmsGoods pmsGoods = pmsGoodsList.get(0);
            Integer stock = pmsGoods.getGoodStock();
            if (pmsGoods.getIsSale() == 0){
                throw new ApiException("商品已下架");
            }
            if (stock > 0 ){
                if (stock < omsCartItem.getQuantity()){
                    throw new ApiException("库存有限,最多可以购买"+stock+"件");
                }else{
                    OmsCartItemExample example = new OmsCartItemExample();
                    OmsCartItemExample.Criteria criteria = example.createCriteria();
                    criteria.andGoodIdEqualTo(goodId).
                            andIsDeletedEqualTo(0).
                            andUserIdEqualTo(umsMemberService.getCurrentMember().getId());
                    OmsCartItem existOmsCartItem = null;
                    // 获取是否有商品
                    List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);
                    // 购物车已经有商品，更新数量
                    if (!omsCartItems.isEmpty()){
                        existOmsCartItem = omsCartItems.get(0);
                        omsCartItem.setId(existOmsCartItem.getId());
                        omsCartItem.setQuantity(existOmsCartItem.getQuantity() + 1);
                        count = omsCartItemMapper.updateByExampleSelective(omsCartItem, example);
                    }else {
                        // 购物车没有商品，添加
                        System.out.println(omsCartItem);
                        count = omsCartItemMapper.insertSelective(omsCartItem);
                    }

                }
            }else {
                throw new ApiException("暂时无货，添加到购物车失败");
            }

        }
        return count ;
    }

    @Override
    public int update(Integer id,Integer quantity) {
        int count = 0;
        OmsCartItemExample example = new OmsCartItemExample();
        OmsCartItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id).andUserIdEqualTo(umsMemberService.getCurrentMember().getId());
        List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);
        if (!omsCartItems.isEmpty()){
            OmsCartItem record = new OmsCartItem();
            record.setId(id);
            record.setQuantity(quantity);
             count = omsCartItemMapper.updateByPrimaryKeySelective(record);
        }
        return count;
    }

    @Override
    public int delete(Integer id) {
        OmsCartItem omsCartItem = new OmsCartItem();
        omsCartItem.setId(id);
        omsCartItem.setIsDeleted(1);
        int count = omsCartItemMapper.updateByPrimaryKeySelective(omsCartItem);
        return count;
    }

    @Override
    public int check(Integer isCheck, Integer cartId) {
        int count = 0;
        OmsCartItem record = new OmsCartItem();
        record.setId(cartId);
        record.setIsCheck(isCheck);
        count = omsCartItemMapper.updateByPrimaryKeySelective(record);
        return count;
    }
}
